package com.mvc;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 委派模式
 */
public class DispatcherServlet extends HttpServlet {

    private Map<String, Method> handlerMapping = new HashMap<>();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doDispatcher(req, resp);
    }

    private void doDispatcher(HttpServletRequest req, HttpServletResponse resp) {
        String uri = req.getRequestURI();
        Method method = handlerMapping.get(uri);

//        method.invoke()
    }

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            handlerMapping.put("/web/getMemeberById.json", MemberController.class.getMethod("getMemeberById", new Class[]{String.class}));

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}
